package com.example.glideprogressiveloadimg.util

import java.io.InputStream

class DecodeProducer {

    private var mProgressiveJpegParser: ProgressiveJpegParser? = null
    private var mProgressiveJpegConfig: ProgressiveJpegConfig? = null
    private var mLastScheduledScanNumber = 0

    constructor() {
        this.mProgressiveJpegParser = ProgressiveJpegParser()
        this.mProgressiveJpegConfig = SimpleProgressiveJpegConfig()
    }

    fun updateDecodeJob(inputStream: InputStream) :Boolean {
        if (!mProgressiveJpegParser!!.parseMoreData(inputStream)) {
            return false
        }
        val scanNum: Int = mProgressiveJpegParser!!.getBestScanNumber()
        if (scanNum <= mLastScheduledScanNumber) {
            // We have already decoded this scan, no need to do so again
                //我们已经解码了这张扫描图，不需要再解码了
            return false
        }
        if (scanNum < mProgressiveJpegConfig!!.getNextScanNumberToDecode(mLastScheduledScanNumber)
            && !mProgressiveJpegParser!!.isEndMarkerRead()
        ) {
            //我们还没有达到配置设置的最小扫描值，还有更多的扫描需要读取(未到达结束标记)。
            // We have not reached the minimum scan set by the configuration and there
            // are still more scans to be read (the end marker is not reached)
            return false
        }
        mLastScheduledScanNumber = scanNum

        return true
    }
}